<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多数据了"
      @load="onLoad"
      :immediate-check="false"
    >
      <art-item
        v-for="item in artList"
        :key="item.art_id.toString()"
        :article="item"
      ></art-item>
    </van-list>
    <!-- <div>{{ kw }}</div> -->
    <!-- <div>{{ this.$router.param.kw }}</div> -->
  </div>
</template>

<script>
import { getSearchResultAPI } from '../../api/searchAPI'
import ArtItem from '../../components/ArtItem/ArtItem.vue'
export default {
  name: 'SearchResult',
  props: ['kw'],
  data () {
    return {
      page: 1,
      artList: [],
      // 是否正在进行上拉加载的数据请求
      loading: false,
      // 所有数据是否已经加载完毕
      finished: false
    }
  },
  components: {
    ArtItem
  },
  created () {
    this.initSearchResult()
  },
  methods: {
    async initSearchResult () {
      const { data: res } = await getSearchResultAPI(this.kw, this.page)
      if (res.message === 'OK') {
        this.artList = [...this.artList, ...res.data.results]
        this.loading = false
        if (res.data.results.length === 0) {
          this.finished = true
        }
      }
    },
    onLoad () {
      this.page++
      this.initSearchResult()
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
